#include "include.h"


int count_next(int curr, int elmer_count) {
     int i, count=0;
     for(i=0;i<elmer_count;i++) {
         if(match(data[curr].seq, data[i].seq) == 0) count++;
     }
     return count;
}
    
int count_next2(int curr, int elmer_count) {
     int i, count=0;
     for(i=0;i<elmer_count;i++) {
         if(!match8(data[curr].seq, data[i].seq) || 
         !match7(data[curr].seq, data[i].seq) || 
         !match6(data[curr].seq, data[i].seq) || 
         !match(data[curr].seq, data[i].seq)) {
                                   count++;
                                   //printf("%s\n   %s", data[curr].seq, data[i].seq);
                                   }
     }
     return count;
}


// zwraca ilosc uzytych elmerow
int get_used(elmer_count) {
    int sum=0, i;
    for(i=0; i<elmer_count; i++) if(data[i].used > 0) sum++;
    return sum;
}


// zwraca sume used
int get_used_count(elmer_count) {
    int sum=0, i;
    for(i=0; i<elmer_count; i++) sum+=data[i].used;
    return sum;
}

// wczytywanie danych z pliku
int wczytaj(char *f_name) {
    FILE *fp;
    char *name = malloc(10+strlen(f_name) * sizeof(char));
    strcpy(name, "instancje/");
    strcat(name, f_name);
    fp = fopen(name, "r");
    if(fp == NULL) {
          perror("BLAD WCZYTYWANIA DANYCH");
          return -1;
          }
    int count=0;
    char tmp[ELMER_LEN];
    while(!feof(fp)) {
        if (fscanf(fp, "%s\n", tmp)) {
            data[count].seq = malloc(ELMER_LEN * sizeof(char));
            strcpy(data[count].seq, tmp);
            data[count].used = 0;
            data[count].next_count = 0;
            data[count].weight = 0;
            count++;
            }
        }
    return count;
}

int match(char *prev, char *next) {
     return strncmp(prev+1, next, ELMER_LEN-1);
}

int match8(char *prev, char *next) {
    return strncmp(prev+2, next, ELMER_LEN-2);
}

int match7(char *prev, char *next) {
    return strncmp(prev+3, next, ELMER_LEN-3);
}

int match6(char *prev, char *next) {
    return strncmp(prev+4, next, ELMER_LEN-4);
}

int match5(char *prev, char *next) {
    return strncmp(prev+5, next, ELMER_LEN-5);
}

int match4(char *prev, char *next) {
    return strncmp(prev+6, next, ELMER_LEN-6);
}

int match3(char *prev, char *next) {
    return strncmp(prev+7, next, ELMER_LEN-7);
}


void QuickSort(struct elmer T[], int Lo, int Hi)
{
   int i,j;
   int x;
   x = T[(Lo+Hi)/2].weight;
   i = Lo;
   j = Hi;
   do
   {
      while (T[i].weight < x) ++i;
      while (T[j].weight > x) --j;
      if (i<=j)
      {
         int tmp = T[i].weight;
         T[i].weight = T[j].weight;
         T[j].weight = tmp;
         ++i; --j;
      }
   } while(i < j);
   if (Lo < j) QuickSort(T, Lo, j);
   if (Hi > i) QuickSort(T, i, Hi);
}    
